<?php
?>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
        "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en">
<head>
  <title>TOUPROPRE DBMS</title>
	<meta http-equiv="content-type" content="text/html; charset=utf-8" />
    <link rel="stylesheet" media="screen" type="text/css" title="Design" href="styles/design.css" />
</head>

<body>
    <!-- TITLE  -->
	<div id="page_title">
      <h1>Update Records</h1>
    </div>

    <!-- Side Menues -->
    <?php include "tools/Menues.html"; ?>

    <!-- Main Body -->
    <div id="page_body">
<?php 
/*
 * Inclusions:
 */
include "tools/ColumnNames.php";
include "tools/Forms.php";
include "tools/Queries.php";

/*
 * Function Definitions:
 */
function ShowNewQueryLink() {
    $add="?table=".$_GET['table'];     
    echo " | <a href=update.php".$add.">New Query</a>";
}
// Describes what to do when 
//    NOT isset($_GET['table') AND NOT isset($_POST['submit'])
function ShowTableSelectForm() {
    echo '<p class="text">This page will allow you to update data to the database.</p>';
    echo "\n<p class='text'>Please select a table:</p>";
    echo "\n<form action='update.php' method='get'>";
    echo "\n<select name='table'>";
    echo "\n<option value='clients'>Clients</option>";
    echo "\n<option value='suppliers'>Suppliers</option>";
    echo "\n<option value='items'>Items</option>";
    echo "\n<option value='sales'>Sales</option>";
    echo "\n<option value='purchases'>Purchases</option>";
    echo "\n<option value='facilities'>Facilities</option>";
    echo "\n</select>";
    echo "\n<input type='submit' name='submit' value='Select'>";
    echo "\n</form>";
}

// Describes what to do when 
//    isset($_GET['table') AND NOT isset($_POST['submit'])
function ShowInputForm() {
    switch($_GET['table']) {
    case 'clients':
    case 'suppliers':
    case 'items':
    case 'facilities':
        $cols = GetInputColumnsArray($_GET['table']);
        EchoColumnsForm('update.php?table='.$_GET['table'], 'submit', 'Update', $cols);
        break;
    case 'sales':
        $before = '<br/>Client ID : <input type="text" name="clientID" />'.
            '<br/>Facility ID : <input type="text" name="facilityID" />'.
            '</br/><br/>';
        global $nitems;
        EchoItemListForm('update.php?table=sales',$nitems, $before,'');
        break;
    case 'purchases':
        $before = '<br/>SupplierID ID : <input type="text" name="supplierID" />'.
            '<br/>Facility ID : <input type="text" name="facilityID" />'.
            '</br/><br/>';
        global $nitems;
        EchoItemListForm('update.php?table=purchases',$nitems, $before,'');
        break;
    default:
        return;
    }
}

// Displays a Hyperlink to return to update.php
function ShowSelectTable() {
    echo "<br/><a href=update.php>Select Table</a>";
}

// Displays a Hyperlink to return to update.php?table=
function ShowAddNewEntry() {
    $params="?table=".$_GET['table'];     
    echo " | <a href=update.php".$params.">Update another entry</a>";
}

function KeepInMind(){
    $columns = GetColumnNames($_GET['table'], false);
    
    $addand=false;
    $n = count($columns);
    for($i=0; $i<$n; $i++) {
      if( $_POST[$columns[$i]] != '' ) {
	if($addand) { $sql = $sql.' AND '; }
	$string = $string."\n<INPUT TYPE=HIDDEN NAME=".$columns[$i]."old VALUE=".$_POST[$columns[$i]].">";
	$addand = true;
      }
    }    
    return $string;
}

function Update(){
  $columns = GetColumnNames($_GET['table'], false);
  $sql = "UPDATE ".$_GET['table']." SET  ";

  $addand=false;
  $n = count($columns);
  for($i=0; $i<$n; $i++) {
    if( $_POST[$columns[$i]] != '' ) {
      if($addand) { $sql = $sql.' , '; }
      $sql = $sql.$columns[$i]."='".$_POST[$columns[$i]]."'";
      $addand = true;
    }
  }

  $sql = $sql."  WHERE  ";

  $addand=false;
  for($i=0; $i<$n; $i++) {
    if( $_POST[$columns[$i].'old'] != '' ) {
      if($addand) { $sql = $sql.' AND '; }
      $sql = $sql.$columns[$i]."='".$_POST[$columns[$i].'old']."'";
      $addand = true;
    }
  }

  return $sql;
}

function BuildQuery($before) {
  $columns = GetColumnNames($_GET['table'], false);
  $sql = $before." FROM ".$_GET['table']." WHERE (";
    
    $addand=false;
    $n = count($columns);
    for($i=0; $i<$n; $i++) {
        if( $_POST[$columns[$i]] != '' ) {
            if($addand) { $sql = $sql.' AND '; }
            $sql = $sql.$columns[$i]."='".$_POST[$columns[$i]]."'";
            $addand = true;
        }
    }
    $sql = $sql . ')';

    return $sql;
}


/*
 * Behaviour :
 */
if(isset($_GET['table'])) {
    if(isset($_POST['submit'])) {
        // Build Query and Display :
        if($_POST['submit'] == 'Search') {
	  $sql = BuildQuery("SELECT *");
	  include "tools/connect.php";
	  $response = mysql_query($sql);
	  $length = mysql_fetch_row($response);
	  if($length == 0) {
	    EchoTableForm('update.php', 'submit', 'Search', $_GET['table'], true);
	    echo "\nNot found ...".mysql_error();
	  }
	  else{
	    $after = KeepInMind();
	    EchoTableFormUpdate('update.php', 'submit', 'Update', $_GET['table'], true, $after);
	  }
        }
	else if($_POST['submit'] == 'Update'){
	  $sql = Update();
	  echo "Update done.";
	}
        else {
	  $sql = "SELECT * FROM ".$_GET['table'];
        }
        
        switch($_GET['table']) {
        case 'purchases':
	  EchoSelectQuery_items($sql, 'itemsPurchased');
	  break;
        case 'sales':
	  EchoSelectQuery_items($sql, 'itemsSold');
	  break;
        default:
	  if($_POST['submit'] != 'Update') EchoSelectQuery($sql);
	  break;
        }
	
        ShowSelectTable(); ShowNewQueryLink();
    }
    else {
        EchoTableForm('update.php', 'submit', 'Search', $_GET['table'], true);
        ShowSelectTable();
    }
 }
 else {
     ShowTableSelectForm();
 }
mysql_close();
?>
        
    </div>
    
    <!-- Footer -->
    <?php include "tools/Footer.html"; ?>
</body>
</html>
